import pycurl, json, StringIO

STREAM_URL = "http://stream.twitter.com/1/statuses/filter.json?locations=-74,40,-73,41"
#-84.7120480,39.0519210,-83.3687549,40.2207590"


class Client:
  def __init__(self):
    self.friends = []
    self.buffer = ""
    self.userid = None
    self.conn = pycurl.Curl()

  def connect(self, username, password):
    self.conn.setopt(pycurl.USERPWD, "%s:%s" % (username, password))
    self.conn.setopt(pycurl.URL, STREAM_URL)
    self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive)
    self.conn.perform()

  def on_receive(self, data):
    self.buffer += data
    if data.endswith("\r\n") and self.buffer.strip():
      content = json.loads(self.buffer)
      self.buffer = ""
      if "text" in content:
      	try:
		print u"{0[user][screen_name]}: {0[text]} {0[coordinates][coordinates]}".format(content)
	except TypeError:
	        print u"{0[user][screen_name]}: {0[text]}".format(content)

client = Client()
client.connect("Tweetographers","uc2302")
